/*
 * GuiUtils.java
 *
 *  created: 12.10.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.util;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.html.HTMLDocument;

/**
 *
 * @author Martin Pokorný
 * @version 0.1
 */
public class GuiUtils {

    /** */
    private GuiUtils() {        
    }


    /**
     * 
     * @param dialog
     * @return 
     */
    public static Action getHideAction(final JDialog dialog) {
        Action hideAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dialog.setVisible(false);
            }
        };
        return hideAction;
    }
    
    /**
     * Přidá akci, která schová okno při stisku klávesy Esc.
     * 
     * @param dialog 
     */
	public static void addHideActionWithEscapeKey(final JDialog dialog) {
        Action hideAction = getHideAction(dialog);
        KeyStroke escape = KeyStroke.getKeyStroke("ESCAPE");        
		dialog.getRootPane().getInputMap(
                JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
		dialog.getRootPane().getActionMap().put("ESCAPE", hideAction);
    }

    /**
     * 
     * @param dialog
     * @return 
     */
    public static Action getDisposeAction(final JDialog dialog) {
        Action disposeAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dialog.dispose();
            }
        };
        return disposeAction;
    }
    
    /**
     * Přidá akci, která zavře okno při stisku klávesy Esc.
     * Zadané okno se zavře metodou {@code dispose()}.
     * 
     * @param dialog
     */
	public static void addDisposeActionWithEscapeKey(final JDialog dialog) {        
        Action disposeAction = getDisposeAction(dialog);
        KeyStroke escape = KeyStroke.getKeyStroke("ESCAPE");        
		dialog.getRootPane().getInputMap(
                JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
		dialog.getRootPane().getActionMap().put("ESCAPE", disposeAction);
    }
    
    /**
     * 
     * @param frame
     * @return 
     */
    public static Action getHideAction(final JFrame frame) {
        Action hideAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                frame.setVisible(false);
            }
        };
        return hideAction;
    }
    
    /**
     * Přidá akci, která schová okno při stisku klávesy Esc.
     * 
     * @param frame
     */
	public static void addHideActionWithEscapeKey(final JFrame frame) {
        Action hideAction = getHideAction(frame);
        KeyStroke escape = KeyStroke.getKeyStroke("ESCAPE");        
		frame.getRootPane().getInputMap(
                JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
		frame.getRootPane().getActionMap().put("ESCAPE", hideAction);
    }    
    
    /**
     * 
     * @param frame
     * @return 
     */
    public static Action getDisposeAction(final JFrame frame) {
        Action disposeAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                frame.dispose();
            }
        };
        return disposeAction;
    }    
    
    /**
     * Přidá akci, která zavře okno při stisku klávesy Esc. 
     * Zadané okno se zavře metodou {@code dispose()}.
     * 
     * @param frame
     */
	public static void addDisposeActionWithEscapeKey(final JFrame frame) {
        Action disposeAction = getDisposeAction(frame);
        KeyStroke escape = KeyStroke.getKeyStroke("ESCAPE");        
		frame.getRootPane().getInputMap(
                JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
		frame.getRootPane().getActionMap().put("ESCAPE", disposeAction);
    } 
    

    /**
     *
     * viz <a href="http://explodingpixels.wordpress.com/2008/10/28/make-jeditorpane-use-the-system-font/">
     * http://explodingpixels.wordpress.com/2008/10/28/make-jeditorpane-use-the-system-font/</a>
     * 
     * @param editorPane
     */
    public static void setDefaultFont(JEditorPane editorPane) {
        // viz http://explodingpixels.wordpress.com/2008/10/28/make-jeditorpane-use-the-system-font/
        // add a CSS rule to force body tags to use the default label font
        // instead of the value in javax.swing.text.html.default.csss
        Font font = UIManager.getFont("Label.font");
        Color fontColor = UIManager.getColor("Label.foreground");
        String bodyRule = "body { font-family: " + font.getFamily() + "; "
                + "font-size: " + font.getSize() + "pt; "
                + "color: #" + ColorUtils.getHTMLColor(fontColor) + "; }";
        ((HTMLDocument)editorPane.getDocument())
                .getStyleSheet().addRule(bodyRule);
    }


    /**
     * 
     * @param editorPane 
     */
    public static void setDefaultFont(JTextArea textArea) {
        Font font = UIManager.getFont("Label.font");
        textArea.setFont(font);     
    }    
    
    /**
     * 
     * @return 
     */
    public static Dimension getPrimaryScreenSize() {
        Toolkit mainToolkit = Toolkit.getDefaultToolkit();
        return mainToolkit.getScreenSize();
    }
    
    /**
     * Aktualizace vzhledu (LAF) všech oken za běhu programu.
     * 
     * @see SwingUtilities#updateComponentTreeUI
     */
    public static void updateAllUI() {
        Frame[] frames = Frame.getFrames();
        for(int i = 0; i < frames.length; i++) {
            SwingUtilities.updateComponentTreeUI(frames[i]);
            frames[i].validate();
            Window[] windows = frames[i].getOwnedWindows();
            for(int w = 0; w < windows.length; w++) {
                SwingUtilities.updateComponentTreeUI(windows[w]);
                windows[w].validate();
            }
        }
    }
    
    /**
     * Zjistí, jestli je zadané okno maximalizované.
     * 
     * @param frame
     * @return 
     */
    public static boolean isMaximized(Frame frame) {
        if (! Toolkit.getDefaultToolkit().isFrameStateSupported(
                Frame.MAXIMIZED_BOTH)) {
            return false;
        }        
        if ((frame.getExtendedState() & Frame.MAXIMIZED_BOTH) ==
                Frame.MAXIMIZED_BOTH) {
            return true;
        }
        return false;
    }
    
    /**
     * Maximalizuje zadané okno.
     *
     * @param frame 
     */
    public static void maximize(Frame frame) {
        if (! Toolkit.getDefaultToolkit().isFrameStateSupported(
                        Frame.MAXIMIZED_BOTH)) {
            return;
        }
        frame.setExtendedState(
                frame.getExtendedState() | Frame.MAXIMIZED_BOTH);
    }

    /**
     *
     * @param pane
     */
    public static void adjustBackground(JEditorPane pane) {
        // (v LaF GTK u JTextPane je nějaká chyba (java 1.6)  ?)
        if(UIManager.getLookAndFeel().getName().toUpperCase().contains("GTK")) {
            pane.setOpaque(false);
            pane.setBackground(UIManager.getColor("TextField.background"));
        }
    }
    
    /**
     * Na chvíli (1 sec) změní kurzor myši na čekací, 
     * po chvíli vrátí zpět kurzor výchozí.
     *
     * @param window
     */
    public static void setWaitCursorForAmoment(final Window window) {
        new Thread() {
            @Override
            public void run() {
                SwingUtilities.invokeLater (new Runnable() {
                    @Override
                    public void run() {
                        window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    }
                });                
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {                    
                }
                SwingUtilities.invokeLater (new Runnable() {
                    @Override
                    public void run() {
                        window.setCursor(Cursor.getDefaultCursor());
                    }
                });                
            }            
        }.start();
    }
    
}   // GuiUtils.java

